package com.leyou.trade.utils;

import com.leyou.auth.dto.UserDetails;
import com.leyou.auth.utils.UserContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class CollectionNameBuilder {

    @Value("${ly.mongo.collectionNamePrefix}")
    private String namePrefix;

    public String build(){
        //获取当前用户
        UserDetails user = UserContext.getUser();
        if (user == null){
            //用户未登录，购物车商品信息都在前端的LocalStorage中，不需要存到集合中，无需生成集合名称
            return "";
        }
        //根据用户id计算hash值，返回集合名称，分配到对应的集合中
        int index = user.getId().hashCode() % 100;
        return namePrefix+index;
    }
}
